package com.slipper.system.organization.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.slipper.common.constants.Constants;
import com.slipper.common.exception.CommonException;
import com.slipper.common.transaction.ReadTransactional;
import com.slipper.common.transaction.WriteTransactional;
import com.slipper.common.util.Tool;
import com.slipper.system.api.organization.bo.OrganizationBO;
import com.slipper.system.organization.dao.OrganizationDao;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

@Service
@ReadTransactional
public class OrganizationService {
    @Resource
    private OrganizationDao organizationDao;
    
    public OrganizationBO queryBy(String id){
        return organizationDao.queryBy(id);
    }

    public List<OrganizationBO> queryList(OrganizationBO bo) {
        return organizationDao.queryList(bo);
    }
    
    public PageInfo<OrganizationBO> queryPage(OrganizationBO bo){
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(organizationDao.queryList(bo));
    }
    
    @WriteTransactional
    public Integer save(OrganizationBO bo){
        int result;
        if (bo.getParentId() == null) {
            bo.setParentId("0");
        }
        if (bo.getId() != null && !"".equals(bo.getId())) {
            result = organizationDao.update(bo);
        } else {
            bo.setId(Tool.getUUID());
            result = organizationDao.insert(bo);
        }
        
        return result;
    }
    
    @WriteTransactional
    public Integer delete(OrganizationBO bo){
        return organizationDao.delete(bo);
    }

    public OrganizationBO queryUnitOrg(String id) {
        if (id == null) {
            return null;
        }
        
        OrganizationBO organization = organizationDao.queryBy(id);
        if (OrganizationBO.TYPE_UNIT.equals(organization.getType())
                || Constants.ROOT_ID.equals(organization.getId())) {
            return organization;
        } else {
            return this.queryUnitOrg(organization.getParentId());
        }
    }

    public OrganizationBO queryByName(OrganizationBO bo) {
        return organizationDao.queryByName(bo);
    }

    @WriteTransactional
    public void saves(List<OrganizationBO> list) {
        StringBuilder builder = new StringBuilder();
        list.sort(Comparator.comparing(OrganizationBO::getShortName));
        for (OrganizationBO bo : list) {
            String result = this.saveItem(bo);
            if (result != null && !result.isEmpty()) {
                builder.append(result).append("@@@");
            }
        }
        if (builder.length() > 0) {
            throw new CommonException(builder.toString());
        }
    }
    
    @WriteTransactional
    public String saveItem(OrganizationBO bo) {
        String[] levelNames = bo.getShortName().split("/");
        if (levelNames.length <= 0) {
            return null;
        }
        String parentLevelName = "";
        for (int i=0;i<levelNames.length - 1;i++) {
            if (i > 0) {
                parentLevelName = parentLevelName.concat("/");
            }
            parentLevelName = parentLevelName.concat(levelNames[i]);
        }

        bo.setShortName(levelNames[levelNames.length-1]);
        this.save(bo);
        return null;
    }

    public int querySizeByType(OrganizationBO bo){
        PageHelper.startPage(1, -1);
        PageInfo<OrganizationBO> pageInfo = new PageInfo<>(organizationDao.queryList(bo));
        return (int) pageInfo.getTotal();
    }

    @WriteTransactional
    public int updateValid(OrganizationBO bo) {
        return organizationDao.updateValid(bo);
    }
}
